const BASE_URL = 'https://api-hmugo-web.itheima.net/api/public/v1/'

// 传参
function request({
    method = 'GET',
    header = {},
    tip = "拼命加载中...",
    url,
    data = {},
    isPullDown = false
}) {
    return new Promise((resolve, reject) => {
        if (!isPullDown) {
            uni.showLoading({
                title: tip,
                mask: true // 加载中不能点击？
            })
        }

        // 获取token
        const token = uni.getStorageSync('token')
        console.log('token  :', token);
        if (token) {
            // header.Authorization = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjU0LCJpYXQiOjE2NDAxMzkxOTgsImV4cCI6MTAwMTY0MDEzOTE5N30.lyCDqe6zpqg9El8ON-X6ICc8aGmFjDeJMkF7w7gLf94'
            header.Authorization = token
        }

        uni.request({
            url: `${BASE_URL}${url}`,// 地址记得拼接
            data,
            header,
            method,
            success: res => {
                resolve(res.data)// res.data才对
            },
            fail: err => {
                reject(err)
            },
            // 不管成功还是失败
            complete() {
                uni.hideLoading()
            }
        })
    })
}

// 挂载到全局的uni上 (记得去main.js引入才算是全局的)
uni.$request = request